#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=1e5+10,MOD=10086;
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int n,head[MAXN],cnt,a[MAXN],ans;
struct edge
{
	int v,next;
}e[MAXN*2];
void addedge(int x,int y)
{
	e[++cnt]=(edge){y,head[x]};
	head[x]=cnt;
	return;
}
int dfs(int u,int fa)
{
	int tmp1[MAXN]={0},tmp2=0;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].v;
		if(v==fa)continue;
		tmp1[++tmp2]=dfs(v,u);
	}
	int ret=a[u];
	for(int i=1;i<=tmp2;++i)
	{
		ans=((LL)ans+tmp1[i])%MOD;
		ret=((LL)ret+tmp1[i]*a[u]%MOD)%MOD;
		for(int j=i+1;j<=tmp2;++j)
			ans=((LL)ans+tmp1[i]*tmp1[j]%MOD*a[u]%MOD)%MOD;
	}
	return ret;
}
int main()
{
	n=read();
	for(int i=1;i<=n;++i)a[i]=read()%MOD;
	for(int i=1;i<n;++i)
	{
		int x=read(),y=read();
		addedge(x,y);
		addedge(y,x);
	}
	printf("%d\n",(int)((LL)dfs(1,0)+ans)%MOD);
	return 0;
}
